//
//  CategoryTypeView.swift
//  KoiPrototype2
//
//  Created by 纪洪文 on 06.03.25.
//

import SwiftUI

struct CategoryTypeView: View {
    
    @Binding var categoryType: CategoryType
    
    var body: some View {
        HStack {
            Button(action: {
                self.categoryType = .expense
            }) {
                Text("支出")
                    .font(.system(size: 14))
                    .bold()
                    .foregroundColor(textForegroundColor(selected: categoryType == .expense))
                    .padding(EdgeInsets(top: 8, leading: 20, bottom: 8, trailing: 20))
                    .background(textBackground(selected: categoryType == .expense))
                    .cornerRadius(40)
            }
            Button(action: {
                self.categoryType = .income
            }) {
                Text("收入")
                    .font(.system(size: 14))
                    .bold()
                    .foregroundColor(textForegroundColor(selected: categoryType == .income))
                    .padding(EdgeInsets(top: 8, leading: 20, bottom: 8, trailing: 20))
                    .background(textBackground(selected: categoryType == .income))
                    .cornerRadius(40)
            }
        }
        .padding(EdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2))
        .background(Color.koiColorBackgroundItem)
        .cornerRadius(50)
            
    }
    
    private func textForegroundColor(selected: Bool) -> Color {
        return selected ? Color.koiColorTitle : Color.koiColorTitleSecond
    }
    
    private func textBackground(selected: Bool) -> Color {
        return selected ? Color.white : Color.white.opacity(0)
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var type :CategoryType = .expense
    CategoryTypeView(categoryType: $type)
}
